package jwlee.testfile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * 文件搜索器
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright:  Copyright (c) 2010</p>
 * <p>Company: DC </p>
 * 
 * @author lijwg
 * @date 2011-7-11
 * @version 1.0
 */
public class FileSearcher {
	/**
	 * 在指定目录下所有jar文件中模糊搜索所给文件
	 * @param dirPath
	 * @param searchFileName
	 * @return
	 * @throws IOException 
	 */
	public static List<String> searchInJars(String dirPath,String searchFileName) throws IOException{
		List<String> existPathList = new ArrayList<String>();
		
		List<File> fileList = FileUtil.getSubFiles(new File(dirPath));
		ZipFile zipFile;
		ZipEntry zipEntry;
		for (File jarFile : fileList) {
			if (jarFile.isDirectory() || !jarFile.getName().endsWith("jar")) {
				continue;// 只处理jar文件
			}
			zipFile = new ZipFile(jarFile);
			Enumeration<? extends ZipEntry> zipEntrys = zipFile.entries();
			while (zipEntrys.hasMoreElements()) {
				zipEntry = (ZipEntry) zipEntrys.nextElement();
				if (zipEntry.isDirectory()) {
					continue;
				}
				String entryFileName = zipEntry.getName().substring(zipEntry.getName().lastIndexOf("/")+1);
				if(entryFileName.indexOf(searchFileName) > -1){
					System.out.println("----"+zipFile.getName()+" : "+zipEntry.getName());
					existPathList.add(zipFile.getName());
					break;
				}
			}
		}
		return existPathList;
	}
	
	public static void main(String[] args) throws IOException {
		String dirPath = "D:\\Oracle\\Middleware\\user_projects\\domains\\adp\\framelib";
		String searchFileName = "user.js";//"ext-all.js";
		List<String> existPathList = FileSearcher.searchInJars(dirPath,searchFileName);
		System.out.println(existPathList);
	}
}
